//
//  WSMallViewController.m
//  WisdomStar
//
//  Created by 谭飞 on 2018/10/23.
//  Copyright © 2018年 zhangliwen. All rights reserved.
//

#import "WSMallViewController.h"

#import "TYCyclePagerView.h"
#import "TYPageControl.h"
#import "WSBannerCell.h"

@interface WSMallViewController ()<TYCyclePagerViewDataSource, TYCyclePagerViewDelegate>

@property (weak, nonatomic) IBOutlet UIView *topView;

@property (nonatomic, strong) TYPageControl *pageControl;

@property (nonatomic, strong) NSArray *images;

@end

@implementation WSMallViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self ws_setup];
    [self ws_addSubviews];
    [self ws_loadData];
}

#pragma mark - Override

- (void)ws_setup {
    
}

- (void)ws_addSubviews {
    
    TYCyclePagerView *cyclePageView = [[TYCyclePagerView alloc] init];
    cyclePageView.isInfiniteLoop = YES;
    cyclePageView.autoScrollInterval = 5.f;
    cyclePageView.delegate = self;
    cyclePageView.dataSource = self;
    [self.topView addSubview:cyclePageView];
    [cyclePageView registerClass:[WSBannerCell class] forCellWithReuseIdentifier:@"pageViewCell"];
    
    TYPageControl *pageControl = [[TYPageControl alloc] init];
    self.pageControl = pageControl;
    pageControl.currentPageIndicatorSize = CGSizeMake(18.f, 8.f);
    pageControl.pageIndicatorSize = CGSizeMake(8.f, 8.f);
    pageControl.currentPageIndicatorTintColor = kThemeGreenColor;
    pageControl.pageIndicatorTintColor = kCommonWhiteBg;
    pageControl.numberOfPages = self.images.count;
    [self.topView addSubview:pageControl];
    
    [cyclePageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.topView);
    }];
    
    [pageControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0.f);
        make.bottom.offset(-10.f);
        make.height.offset(20.f);
    }];
}

- (void)ws_loadData {
    
}

#pragma mark - TYCyclePagerViewDataSource, TYCyclePagerViewDelegate

- (NSInteger)numberOfItemsInPagerView:(TYCyclePagerView *)pageView {
    return self.images.count;
}

- (UICollectionViewCell *)pagerView:(TYCyclePagerView *)pagerView cellForItemAtIndex:(NSInteger)index {
    WSBannerCell *cell = [pagerView dequeueReusableCellWithReuseIdentifier:@"pageViewCell" forIndex:index];
    cell.imageView.image = WSImageMake(self.images[index]);
    return cell;
}

- (TYCyclePagerViewLayout *)layoutForPagerView:(TYCyclePagerView *)pageView {
    TYCyclePagerViewLayout *layout = [[TYCyclePagerViewLayout alloc] init];
    layout.itemSize = CGSizeMake(pageView.xmg_width, pageView.xmg_height);
    layout.itemSpacing = 0.f;
    layout.itemHorizontalCenter = YES;
    return layout;
}

- (void)pagerView:(TYCyclePagerView *)pageView didScrollFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex {
    self.pageControl.currentPage = toIndex;
}


#pragma mark - Getter

- (NSArray *)images {
    if (_images == nil) {
        _images = @[@"img_banner", @"img_banner1"];
    }
    return _images;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
